/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */

package com.environment_pro.monitor.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;

import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.environment_pro.monitor.mapper.TbSystemJobMapper;
import com.environment_pro.monitor.model.TbSystemJob;
import com.environment_pro.monitor.scheduler.mgr.QuartzScheduleMgr;
import com.environment_pro.monitor.service.TbSystemJobService;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageInfo;
import com.environment_pro.monitor.util.PageHelper.Page;
import com.environment_pro.monitor.util.StaticUtil;

/**
 * @author yangfeng 
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional  //此处不再进行创建SqlSession和提交事务，都已交由spring去管理了。
public class TbSystemJobServiceImpl implements TbSystemJobService {
	
	@Resource
	private TbSystemJobMapper mapper;
	
	@Autowired
	private QuartzScheduleMgr quartzScheduleMgr;
	
	@Autowired
	private SchedulerFactory schedulerFactory;

	//移除缓存
	//@CacheEvict(value = "tbSystemJobCache",key="'id'+#id")
	public boolean delete(int id) {
		
		return mapper.delete(id);
	}
	
	public List<TbSystemJob> findAll(TbSystemJob tbSystemJob) {
		List<TbSystemJob> findAllList = mapper.findAll(tbSystemJob);
		return findAllList;
	}

	@SuppressWarnings("unchecked")
	public Page<TbSystemJob> findAll(PageInfo pageInfo, TbSystemJob tbSystemJob) {
		PageHelper.startPage(pageInfo);  
		mapper.findAll(tbSystemJob);
		return PageHelper.endPage();
	}

	//使用缓存
	//@Cacheable(value = "tbSystemJobCache",key="'id'+#id")
	public TbSystemJob findById(int id) {
		TbSystemJob tbSystemJob = mapper.findById(id);
		return tbSystemJob;
	}
	
	//缓存数据
	//@CachePut(value = "tbSystemJobCache",key="'id'+#tbSystemJob.id")
	public void save(TbSystemJob tbSystemJob) {
		tbSystemJob.setAddTime(new Date());
		mapper.save(tbSystemJob);
	}
	//更新缓存
	//@CacheEvict(value = "tbSystemJobCache",key="'id'+#tbSystemJob.id")
	public boolean update(TbSystemJob tbSystemJob) {

		return mapper.update(tbSystemJob);
	}

	//编码转化为名称
	public String code2name(String code) {
		// TODO Auto-generated method stub
		return null;
	}

	//ID转化为名称
	public String id2name(int id) {
		
		return null;
	}
	
	/**
	 * 停止任务
	 * 
	 * @param jobKeys
	 * @return
	 * @throws SchedulerException 
	 * @throws SchedulerException
	 */
	public boolean stopJob(int id) {
		TbSystemJob tbSystemJob = mapper.findById(id);
		tbSystemJob.setJobStatus("Stop");
		try {
			quartzScheduleMgr.setScheduler(schedulerFactory.getScheduler());
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		quartzScheduleMgr.stopJob(tbSystemJob);
		return mapper.update(tbSystemJob);
	}
	
	/**
	 * 启动任务
	 * @param tbSystemJob
	 * @throws SchedulerException 
	 */
	public boolean startJob(int id) throws SchedulerException{
		TbSystemJob tbSystemJob = mapper.findById(id);
		tbSystemJob.setJobStatus("Start");
		quartzScheduleMgr.setScheduler(schedulerFactory.getScheduler());
		quartzScheduleMgr.startJob(tbSystemJob);
		return mapper.update(tbSystemJob);
	}
	
	/**
	 * 系统启动是初始化job
	 * @throws SchedulerException 
	 */
	public void initSchedulerJob() throws SchedulerException {
		//QuartzScheduleMgr quartzScheduleMgr = (QuartzScheduleMgr) StaticUtil.getBean("quartzScheduleMgr");
		//QuartzScheduleMgr quartzScheduleMgr = new QuartzScheduleMgr();
		//获取调度工厂类
		//SchedulerFactory schedulerFactory = (SchedulerFactory) StaticUtil.getBean("stdSchedulerFactory");
		quartzScheduleMgr.setScheduler(schedulerFactory.getScheduler());
		//获取所有要启动的job
		TbSystemJob tbSystemJob = new TbSystemJob();
		tbSystemJob.setJobStatus("start");
		List<TbSystemJob> tbSystemJobList= this.findAll(tbSystemJob);
		for (Iterator<TbSystemJob> itr = tbSystemJobList.iterator(); itr.hasNext();) {
			quartzScheduleMgr.startJob(itr.next());
		}
	}

}